package com.quectel.cms.controller.carparking;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.carparking.dto.CarParkingDoorDto;
import com.quectel.core.module.carparking.service.CarParkingDoorService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author bobo
 * @email bob.yu@quectel.com
 * @date 2022/02/22 09:44
 */
@RestController
@RequestMapping("carParkingDoor")
@Tag(name = "CarParkingDoorController", description = "停车场出入口管理表相关api")
public class CarParkingDoorController extends BaseController {

    @DubboReference
    private CarParkingDoorService carParkingDoorService;

    /**
     * 列表
     */
    @GetMapping("queryByCarParkingId")
    @Operation(summary = "园区筛选停车场出入口")
    public Response<List<CarParkingDoorDto>> queryByCarParkingId(
            @Parameter(description = "停车场出入口id") @RequestParam Long carParkingId
    ) {
        return Response.<List<CarParkingDoorDto>>ok().wrap(carParkingDoorService.selectByVillageId(carParkingId));
    }


    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "carParkingDoor:list")
    @Operation(summary = "分页列表")
    public Response<Page<CarParkingDoorDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = carParkingDoorService.queryTotal(request.getParams());

        List<CarParkingDoorDto> list = null;
        if (total > 0) {
            list = carParkingDoorService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<CarParkingDoorDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<CarParkingDoorDto>>ok().wrap(page);
    }


    /**
     * 编辑反显查询
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "carParkingDoor:edit")
    @Operation(summary = "查询单条记录")
    public Response<CarParkingDoorDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (carParkingDoorService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        CarParkingDoorDto carParkingDoorDto = carParkingDoorService.selectById(id);

        return Response.<CarParkingDoorDto>ok().wrap((carParkingDoorDto));
    }

    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "carParkingDoor:info")
    @Operation(summary = "查询单条记录")
    public Response<CarParkingDoorDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (carParkingDoorService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        CarParkingDoorDto carParkingDoorDto = carParkingDoorService.selectById(id);

        return Response.<CarParkingDoorDto>ok().wrap((carParkingDoorDto));
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "carParkingDoor:edit")
    @Operation(summary = "保存单条记录")
    public Response<Object> save(@RequestBody @Validated CarParkingDoorDto carParkingDoorDto) {

        //填充基础数据
        fillSaveCommonData(carParkingDoorDto);

        CarParkingDoorDto dbValue = carParkingDoorService.selectOne(carParkingDoorDto.getDeviceId());
        if (dbValue != null) {
            return Response.error("停车设备已被占用");
        }

        carParkingDoorService.save(carParkingDoorDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "carParkingDoor:edit")
    @Operation(summary = "修改单条记录")
    public Response<Object> update(@RequestBody @Validated CarParkingDoorDto carParkingDoorDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(carParkingDoorDto.getId()));
        if (carParkingDoorService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        CarParkingDoorDto dbValue = carParkingDoorService.selectOne(carParkingDoorDto.getDeviceId());
        if (dbValue != null && !Objects.equals(carParkingDoorDto.getId(), dbValue.getId())) {
            return Response.error("停车设备已被占用");
        }

        if (dbValue != null && !Objects.equals(carParkingDoorDto.getCode(), dbValue.getCode())) {
            return Response.error("出入口编码已存在");
        }

        //填充基础数据
        fillEditCommonData(carParkingDoorDto);

        carParkingDoorService.updateById(carParkingDoorDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "carParkingDoor:delete")
    @Operation(summary = "批量删除记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (carParkingDoorService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        carParkingDoorService.deleteBatch(ids);

        return Response.ok();
    }
}
